var util = require('../../../../utils/util.js');
var api = require('../../../../config/api.js');
import Notify from '../../../../lib/vant-dist/notify/notify';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    notVIP: '', //判断是否是有效会员
    integral: 0, //积分
    limit: 0, //可提现额度
    bankCardMsg: '请选择银行卡',
    txIntegral: null, //提现积分,
    ServiceCharge: 0, //手续费
    actualArrival: 0, //实际到账
    remarks: '', //备注
    cardNum: null, //提现账户银行卡卡号
  },

  //选择银行卡
  goSelectCard() {
    wx.navigateTo({
      url: '/pages/ucenter/bankCard/selectCard/index',
    })
  },
  //提现积分输入框
  changeTxIntegral({
    detail
  }) {
    var rep = /[.]/;
    if (detail.value > this.data.integral) {
      this.setData({
        txIntegral: null
      })
      Notify({
        type: 'warning',
        message: '提现积分不得超过总积分'
      });
    } else if (detail.value > this.data.limit) {
      this.setData({
        txIntegral: null
      })
      Notify({
        type: 'warning',
        message: '提现积分不得超过可提现额度'
      });
    } else if (detail.value < 100) {
      this.setData({
        txIntegral: null
      })
      Notify({
        type: 'warning',
        message: '提现积分最少为100'
      });
    } else if (rep.test(detail.value)) {
      this.setData({
        txIntegral: null
      })
      Notify({
        type: 'warning',
        message: '提现积分必须为整数'
      });
    } else {
      this.setData({
        txIntegral: detail.value,
        ServiceCharge: Math.floor(detail.value * 0.05 * 100) / 100
      })
    }
  },
  //备注栏
  remarks({
    detail
  }) {
    this.setData({
      remarks: detail.value
    })
  },
  //立即提现
  withdrawal() {
    let data = {
      fromId: wx.getStorageSync('userid'),
      integral: this.data.txIntegral * 1,
      remark: this.data.remarks,
      yhk: this.data.cardNum,
      yhklx: this.data.bankCardMsg.split('(')[0],
      sxflntegral: this.data.ServiceCharge * 1,
      relintegral: (this.data.txIntegral - this.data.ServiceCharge) * 1,
    }
    if (data.integral && data.yhk && data.yhklx) {
      util.request(`${api.IntegralWithdrawal}?fromId=${data.fromId}&integral=${data.integral}&remark=${data.remark}&yhk=${data.yhk}&yhklx=${data.yhklx}&sxfIntegral=${data.sxflntegral}&relintegral=${data.relintegral}`, {}, 'post').then(res => {
        if (!res.errno) {
          wx.showToast({
            title: '提现成功',
            icon: 'success',
            mask: true
          })
          setTimeout(() => {
            wx.switchTab({
              url: '/pages/ucenter/index/index',
            })
          }, 1500);
        } else {
          wx.showToast({
            title: res.errmsg,
            icon: 'none',
            mask: true
          })
        }
      }).catch((err) => {
        wx.showToast({
          title: err,
          icon: 'none',
          mask: true
        })
      })
    } else {
      Notify({
        type: 'warning',
        message: '请正确填写提现信息'
      });
    }
  },
  //获取默认银行卡
  getDefaultBankCard() {
    //根据用户选择的银行卡或默认银行卡来动态展示提现账户简略信息
    let enterCard = wx.getStorageSync('enterBankCard')
    if (enterCard) {
      enterCard = JSON.parse(enterCard)
      let yhkkh = enterCard.yhkkh.slice(-4)
      let bankCardMsg = enterCard.yhktype.concat(`(${yhkkh})`)
      this.setData({
        bankCardMsg,
        cardNum: enterCard.yhkkh
      })
    } else {
      util.request(api.BankCardDefault).then(res => {
        if (res.data == '暂无默认银行卡') {
          this.setData({
            bankCardMsg: "请选择银行卡"
          })
        } else {
          let yhkkh = res.data.yhkkh.slice(-4)
          let bankCardMsg = res.data.yhktype.concat(`(${yhkkh})`)
          this.setData({
            bankCardMsg,
            cardNum: res.data.yhkkh
          })
        }
      })
    }
  },
  //获取总积分和可提现额度
  getWithdrawallimit() {
    let that = this
    util.request(api.IntegralWithdrawallimit).then(res => {
      if (!res.errno) {
        this.setData({
          integral: res.data.total,
          limit: res.data.ketixian
        })
      } else {
        wx.showToast({
          title: res.errmsg,
          icon: 'none',
          duration: 3000,
          mask: true,
          success() {
            that.setData({
              notVIP: res.errmsg
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getWithdrawallimit()
    this.getDefaultBankCard()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.removeStorageSync('enterBankCard')
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})